package com.cloudm.technician.ui.base

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.support.v7.app.AlertDialog
import com.cloudm.technician.R

/**
 * 作者: wesley
 * 时间: 2018/1/3
 * 描述: 对话框实现
 */
class PromptDialogFragment : BaseDialogFragment(), DialogInterface.OnClickListener {

    private var message: Int = 0
    private var positiveButton: Int = 0
    private var negativeButton: Int = 0
    private var title: Int = 0
    var listener: OnDialogItemClickListener? = null

    companion object {
        fun newInstance(message: Int, positiveButton: Int = R.string.ok,
                        negativeButton: Int = R.string.cancel,
                        title: Int = R.string.prompt): PromptDialogFragment {
            return PromptDialogFragment().apply {
                arguments = Bundle().apply {
                    putInt("message", message)
                    putInt("positiveButton", positiveButton)
                    putInt("negativeButton", negativeButton)
                    putInt("title", title)
                }
            }
        }
    }

    override fun handleBundle(bundle: Bundle) {
        message = bundle.getInt("message")
        positiveButton = bundle.getInt("positiveButton")
        negativeButton = bundle.getInt("negativeButton")
        title = bundle.getInt("title")
    }

    override fun createDialog(): Dialog {
        return AlertDialog.Builder(mContext).setMessage(message).setPositiveButton(positiveButton,
                this).setNegativeButton(negativeButton, this).create()
    }

    override fun onDetach() {
        super.onDetach()
        listener = null
    }

    override fun onClick(dialog: DialogInterface?, which: Int) {
        dismiss()
        when (which) {
            -1 -> listener?.onPositiveButtonClick()
        }
    }


    interface OnDialogItemClickListener {

        fun onPositiveButtonClick()
    }
}